package com.itqf.Controller;

import com.github.pagehelper.PageInfo;
import com.itqf.Enum.CodeEnum;
import com.itqf.Service.Ordertopic.OrderTopicService;
import com.itqf.Service.Service.ServicerService;
import com.itqf.Service.Serviceanswer.ServiceanswerService;
import com.itqf.Util.DTO.ResultDTO;
import com.itqf.Util.JsonUtil;
import com.itqf.Util.UserIdTestUtil;
import com.itqf.Util.myPageInfo;
import com.itqf.Util.requestHeader.RequestHeaderUtil;
import com.itqf.entity.Order;
import com.itqf.entity.Serviceanswer;
import com.itqf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrdertopicController
{
    @Resource
    private OrderTopicService orderTopicServiceo ;
    @Resource
    private ServicerService servicerService;
    @Resource
    private ServiceanswerService serviceanswerService;



    /**
    * @Description:  订单查找
    * @Param: [pageindex, topicState, token]
    * @return: com.github.pagehelper.PageInfo
    * @Author: 陈岗阳
    * @Date: 2019/10/17 
    */
    @RequestMapping(value = "/huileme/a/u/wode/OrdertopicController/myOrder",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<PageInfo> FindOrderByToken(@RequestParam(defaultValue = "1")int pageindex, @RequestParam(defaultValue = "-1")int topicState, HttpServletRequest request)
    {
//        调用获取请求头中的工具类
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();

        PageInfo pageInfo=orderTopicServiceo.FindByLike(pageindex, myPageInfo.PAGESIZE,topicState, userid1);

          ResultDTO resultDTO=new ResultDTO();

            if (!pageInfo.getList().isEmpty())
            {
                resultDTO.setData(pageInfo);
                resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
                resultDTO.setCode(CodeEnum.selectsuccess.getCode());
            }else
            {
                resultDTO.setData(pageInfo);
                resultDTO.setMessage(CodeEnum.selecterror.getMsg());
                resultDTO.setCode(CodeEnum.selecterror.getCode());
            }
        return resultDTO;
    }


    /***
     *
     * 我的售后
     */
    @RequestMapping(value = "/huileme/a/u/wode/OrdertopicController/afterSale",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO AfterSale(@RequestParam(defaultValue = "1")int pageindex, @RequestParam(defaultValue = "-1")int topicState,
                             HttpServletRequest request,@RequestParam(defaultValue = "-1")int ordertopicid)
    {
//        调用获取请求头中的工具类
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();


        ResultDTO resultDTO=new ResultDTO();
        if (ordertopicid==-1)
        {
           PageInfo   pageInfo = orderTopicServiceo.FindByLike(pageindex, myPageInfo.PAGESIZE, topicState,userid1);
            if (!pageInfo.getList().isEmpty())
            {
                resultDTO.setData(pageInfo);
                resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
                resultDTO.setCode(CodeEnum.selectsuccess.getCode());
            }else
            {
                resultDTO.setData(pageInfo);
                resultDTO.setMessage(CodeEnum.selecterror.getMsg());
                resultDTO.setCode(CodeEnum.selecterror.getCode());
            }
            return resultDTO;

        }else {
            //进行退款操作
            Boolean b=orderTopicServiceo.deleteByTopicId(ordertopicid);

            if (b)
            {
                resultDTO.setMessage("您的申请已提交 三个工作日内退款到位");
                resultDTO.setCode(CodeEnum.deletesuccess.getCode());
            }else {
                resultDTO.setMessage("您的退款失败 请您联系客服");
                resultDTO.setCode(CodeEnum.deleteerror.getCode());
            }
            return resultDTO;
              }

    }


    /**
     *
     * 我的客服
     * */
 @RequestMapping(value = "/huileme/a/u/wode/OrdertopicController/service",method = RequestMethod.GET)
 @ResponseBody
 public ResultDTO Service(@RequestParam(defaultValue = "1")int pageindex,
                          @RequestParam(defaultValue = "-1")int serviceId, String userService, HttpServletRequest request , HttpSession session)
 {



     ResultDTO resultDTO=new ResultDTO();

     final String test = (String)session.getAttribute("test");
     //判断用户是否是初次登陆售后

     if (test==null)
        {

            RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
            final User getheader = requestHeaderUtil.getheader(request);
            final String userid1 = getheader.getUserid();
//         初次调用的时候 展示用户之前的记录
            PageInfo pageInfo=servicerService.FindLikeByserviceId(pageindex,myPageInfo.PAGESIZE,userid1);
            if (!pageInfo.getList().isEmpty())
            {
                resultDTO.setData(pageInfo);
                resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
                resultDTO.setCode(CodeEnum.selectsuccess.getCode());
            }else
            {
                resultDTO.setData(pageInfo);
                resultDTO.setMessage(CodeEnum.selecterror.getMsg());
                resultDTO.setCode(CodeEnum.selecterror.getCode());
            }

            resultDTO.setMap("您好! 这里是会了么客服 请问有什么能帮助到您的");
//            获得问题列表
            List<Serviceanswer> serviceanswerList= serviceanswerService.Findall();

            resultDTO.setList(serviceanswerList);
//进行到这一步 设置参数 让他再次请求时不在发展示目录
            session.setAttribute("test","test");

            return resultDTO;
        }else{
//            通过用户选择的id直接获取自动回答的答案
            if(serviceId!=-1)
            {
               Serviceanswer serviceanswer= serviceanswerService.FindByserviced(serviceId);

               if (serviceanswer!=null)
               {
                   resultDTO.setData(serviceanswer);
                   resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
                   resultDTO.setCode(CodeEnum.selectsuccess.getCode());
               }else
               {
                   resultDTO.setMessage(CodeEnum.selecterror.getMsg());
                   resultDTO.setCode(CodeEnum.selecterror.getCode());
               }
               return resultDTO;
//                存储用户的提问内容
            }else if(userService!=null)
            {


                //        调用获取请求头中的工具类
                RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
                final User getheader = requestHeaderUtil.getheader(request);
                final String userid1 = getheader.getUserid();



              int count=servicerService.InsertuserService(userService,userid1);

              if (count>0)
              {

                  resultDTO.setCode(CodeEnum.addsuccess.getCode());
                  resultDTO.setMessage("用户提问保存成功");
                  resultDTO.setMap("尊敬的用户你好 马上为您转人工客服,请稍等");
                  return resultDTO;
              }
            }
        }
        return resultDTO;

 }


}
